package com.pda.yuanjing.pages.jdpd.add;

import android.content.Context;

import com.apkfuns.logutils.LogUtils;
import com.pda.yuanjing.beanin.AddCBillBeanIn;
import com.pda.yuanjing.beanout.BaseBeanOut;
import com.pda.yuanjing.beanout.PDPBeanOut;
import com.public_module.basemvp.model.BaseModel;
import com.public_module.config.SetConfig;
import com.public_module.tools.StringProcess;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;

public class AddBillModelImpl extends BaseModel implements AddBillModel {
    private String TAG = "AddBillModelImpl";
    private BaseBeanOut baseBeanOut;
    private PDPBeanOut pdpBeanOut;

    public AddBillModelImpl(Context context) {
        super(context);
    }

    @Override
    public void addBill(AddCBillBeanIn addCBillBeanIn, CallBack callBack) {
        try {
            RequestBody body = RequestBody.create(SetConfig.JSON, gson.toJson(addCBillBeanIn));
            api.downloadBill(body)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new io.reactivex.Observer<ResponseBody>() {
                        private Disposable mDisposable;

                        @Override
                        public void onSubscribe(Disposable d) {
                            mDisposable = d;
                        }

                        @Override
                        public void onComplete() {
                            callBack.ontip("");
                            mDisposable.dispose();
                        }

                        @Override
                        public void onError(Throwable e) {
                            if (baseBeanOut != null && baseBeanOut.getErrmsg() !=null) {
                                callBack.ontip(baseBeanOut.getErrmsg());
                            } else {
                                callBack.ontip(SetConfig.connectError);
                            }
                            mDisposable.dispose();
                        }

                        @Override
                        public void onNext(ResponseBody abBean) {
                            LogUtils.w(StringProcess.responsetoString(abBean));
                            baseBeanOut = gson.fromJson(StringProcess.responsetoString(abBean), BaseBeanOut.class);
                            if (baseBeanOut != null && baseBeanOut.getErrcode() == 0) {
                                onComplete();
                            } else {
                                onError(new Throwable());
                            }
                        }
                    });
        } catch (Exception e) {
            callBack.ontip(SetConfig.connectError);
            e.printStackTrace();
        }
    }

    @Override
    public void getPlist(AddCBillBeanIn addCBillBeanIn, CallBack callBack) {
        try {
            RequestBody body = RequestBody.create(SetConfig.JSON, gson.toJson(addCBillBeanIn));
            api.downloadBill(body)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new io.reactivex.Observer<ResponseBody>() {
                        private Disposable mDisposable;

                        @Override
                        public void onSubscribe(Disposable d) {
                            mDisposable = d;
                        }

                        @Override
                        public void onComplete() {
                            callBack.ontipS(pdpBeanOut.getData());
                            mDisposable.dispose();
                        }

                        @Override
                        public void onError(Throwable e) {
                            if (pdpBeanOut != null && pdpBeanOut.getErrmsg() !=null) {
                                callBack.ontip(pdpBeanOut.getErrmsg());
                            } else {
                                callBack.ontip(SetConfig.connectError);
                            }
                            mDisposable.dispose();
                        }

                        @Override
                        public void onNext(ResponseBody abBean) {
                            LogUtils.w(StringProcess.responsetoString(abBean));
                            pdpBeanOut = gson.fromJson(StringProcess.responsetoString(abBean), PDPBeanOut.class);
                            if (pdpBeanOut != null && pdpBeanOut.getErrcode() == 0) {
                                onComplete();
                            } else {
                                onError(new Throwable());
                            }
                        }
                    });
        } catch (Exception e) {
            callBack.ontip(SetConfig.connectError);
            e.printStackTrace();
        }
    }
}

